﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Departmentss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Clientss
{
    public class AddClienHander : IRequestHandler<AddClienComand, Result>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Clients> clientsrepository;

        public AddClienHander(IMapper mapper, IBaseRepository<Clients> clientsrepository)
        {
            this.mapper = mapper;
            this.clientsrepository = clientsrepository;
        }
        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public  async Task<Result> Handle(AddClienComand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Clients entity = mapper.Map<Clients>(request);
                var res = await clientsrepository.Add(entity);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加客户成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "添加客户异常" + ex.Message;
            }
            return result;
        }
    }
}
